package com.yztc.ui.cook.categoryinfo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.yztc.bean.cook.CategoryInfoBean;
import com.yztc.bean.cook.CookbooklistBean;
import com.yztc.chunboapp.R;
import com.yztc.ui.WebViewActivity;
import com.yztc.utils.BaseAcitivity;

import java.util.List;


/**
 * Created by Administrator on 2016/10/10.
 */

public class CategoryInfoActivity extends BaseAcitivity implements CategoryInfoConstract.View {
    private CategoryInfoPresenter presenter;
    private ListView lv_category;
    private TextView tv_toolbar_category;
    private CategoryInfoAdapter adapter;
    private ImageView iv_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);

        init();
        presenter = new CategoryInfoPresenter(this);
        presenter.getCategoryInfo();
    }

    private void init() {
        lv_category= (ListView) findViewById(R.id.listVIew_category);
        iv_back= (ImageView) findViewById(R.id.back_toolbar_category);
        tv_toolbar_category= (TextView) findViewById(R.id.tv_toolbar_category);

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onCategoryInfoSuccess(CategoryInfoBean categoryInfoBean) {
        final List<CookbooklistBean> cookbooklistBean=categoryInfoBean.getCookbooklist();
        if(categoryInfoBean.getTitle()!=null&&categoryInfoBean.getTitle()!="")
        {tv_toolbar_category.setText(categoryInfoBean.getTitle());}
        else {tv_toolbar_category.setText("全部分类");}
        adapter=new CategoryInfoAdapter(this,cookbooklistBean);
        lv_category.setAdapter(adapter);

        lv_category.setLayoutAnimation(getListAnim());

        lv_category.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(CategoryInfoActivity.this,WebViewActivity.class);
                Bundle bundle = new Bundle();
                Log.i("wx","------URL------>"+cookbooklistBean.get(position).getUrl());
                bundle.putString("link", cookbooklistBean.get(position).getUrl());
                bundle.putString("title","菜谱详情");
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onCategoryInfoFail(Throwable error) {

    }

    public static LayoutAnimationController getListAnim() {
        AnimationSet set = new AnimationSet(true);
        Animation animation = new AlphaAnimation(0.0f, 1.0f);
        animation.setDuration(500);
        set.addAnimation(animation);

        animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        animation.setDuration(1000);
        set.addAnimation(animation);
        LayoutAnimationController controller = new LayoutAnimationController(
                set, 0.5f);
        return controller;
    }
}
